<template>
  <div>
    <el-card class="login-form-layout">
      <el-form
        autocomplete="on"
        :model="loginForm"
        :rules="loginRules"
        ref="loginForm"
        label-position="left"
      >
        <div style="text-align: center">
          <svg-icon icon-class="login-mall " style="width: 56px;height: 56px;color: #409EF"></svg-icon>
        </div>
        <h2 class="login-title color-main">验证码登陆</h2>
        <el-form-item prop="username">
          <el-input
            name="username"
            type="text"
            v-model="loginForm.username"
            autocomplete="on"
            placeholder="请输入用户名"
          >
            <span slot="prefix">
              <svg-icon icon-class="user" class="color-main"></svg-icon>
            </span>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            name="password"
            type="text"
            @keyup.enter.native="handleLogin"
            v-model="loginForm.authCode"
            autocomplete="on"
            placeholder="请输入密码"
          >
            <span slot="prefix">
              <svg-icon icon-class="password" class="color-main"></svg-icon>
            </span>
            <span slot="suffix" @click="handleGetAutoCode">
              <label class="color-main">获取验证码</label>
            </span>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button
            type="text"
            style="float: right"
            @click.native.prevent="handlePasswordLogin"
          >密码登陆</el-button>
        </el-form-item>
        <el-form-item style="margin-bottom: 10px">
          <el-button
            style="width: 100%"
            type="primary"
            :loading="loading"
            @click.native.prevent="handleLogin"
          >立即登录</el-button>
        </el-form-item>
        <el-form-item style="margin-bottom: 60px">
          <el-button style="width: 100%" type="primary" @click.native.prevent="handleRegist">免费注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <img :src="login_center_bg" class="login-center-layout" />
  </div>
</template>

<script>
  import {isvalidUsername} from '@/utils/validate';
  import {setSupport,getSupport,SupportUrl} from '@/utils/support';
  import login_center_bg from '@/assets/images/login_center_bg.png'
  import {register,getAuthCode} from '@/api/login'

  export default {
    name: 'login',
    data() {
      const validateUsername = (rule, value, callback) => {
        if (!isvalidUsername(value)) {
          callback(new Error('请输入正确的用户名'))
        } else {
          callback()
        }
      };
      const validatePass = (rule, value, callback) => {
        if (value.length < 3) {
          callback(new Error('密码不能小于3位'))
        } else {
          callback()
        }
      };
      return {
        loginForm: {
          username: '13923497592',
          password: '123456',
          telephone: '13923497592',
          authCode: '',
          reccode: '',
        },
        loginRules: {
          username: [{required: true, trigger: 'blur', validator: validateUsername}],
          password: [{required: true, trigger: 'blur', validator: validatePass}]
        },
        loading: false,
        pwdType: 'password',
        login_center_bg
      }
    },
    methods: {
      handleGetAutoCode() {
         this.getAuthCode = true;
            getAuthCode(this.loginForm.telephone).then(response => {
              this.loginForm.authCode = response.data
            this.listLoading = false;
        })
       
      },
      handleLogin() {
         this.$refs.loginForm.validate(valid => {
          if (valid) {
           
            this.loading = true;
            this.$store.dispatch('SmsLogin', this.loginForm).then(() => {
              this.loading = false;
              this.$router.push({path: '/'})
            }).catch(() => {
              this.loading = false
            })
          } else {
            console.log('参数验证不合法！');
            return false
          }
        })
      },
      handleRegist(){
        this.$router.push({path: '/regist'})
      },
      handlePasswordLogin(){
        this.$router.go(-1);//返回上一层
      },
    }
  }
</script>

<style scoped>
.login-form-layout {
  position: absolute;
  left: 15px;
  right: 15px;
  max-width: 300px;
  margin: 140px auto;
}

.login-title {
  text-align: center;
}

.login-center-layout {
  background: #409eff;
  width: 100%;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  margin-top: 200px;
}
</style>
